 package handling.world.guild;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.world.WorldAllianceService;
 import handling.world.WorldGuildService;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collection;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.MaplePacketCreator;
 import tools.packet.GuildPacket;


 public class MapleGuildAlliance
   implements Serializable
 {
   public static final long serialVersionUID = 24081985245L;
   public static final int CHANGE_CAPACITY_COST = 10000000;
/*  24 */   private static final Logger log = LogManager.getLogger(MapleGuildAlliance.class);
/*  25 */   private final int[] guilds = new int[5];
   private int allianceid;
   private int leaderid;
/*  28 */   private String[] ranks = new String[5];
   private int capacity;

   public MapleGuildAlliance(int id) {
/*  32 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  33 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM alliances WHERE id = ?");
/*  34 */       ps.setInt(1, id);
/*  35 */       ResultSet rs = ps.executeQuery();
/*  36 */       if (!rs.first()) {
/*  37 */         rs.close();
/*  38 */         ps.close();
/*  39 */         this.allianceid = -1;
         return;
       }
/*  42 */       this.allianceid = id;
/*  43 */       this.name = rs.getString("name");
/*  44 */       this.capacity = rs.getInt("capacity");
/*  45 */       for (int i = 1; i < 6; i++) {
/*  46 */         this.guilds[i - 1] = rs.getInt("guild" + i);
/*  47 */         this.ranks[i - 1] = rs.getString("rank" + i);
       }
/*  49 */       this.leaderid = rs.getInt("leaderid");
/*  50 */       this.notice = rs.getString("notice");
/*  51 */       rs.close();
/*  52 */       ps.close();
/*  53 */     } catch (SQLException se) {
/*  54 */       log.error("[MapleGuildAlliance] 从数据库中加载家族联盟信息出错." + se);
     }
   }
   private String name; private String notice;
   public static Collection<MapleGuildAlliance> loadAll() {
/*  59 */     Collection<MapleGuildAlliance> ret = new ArrayList<>();

/*  61 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  62 */       PreparedStatement ps = con.prepareStatement("SELECT id FROM alliances");
/*  63 */       ResultSet rs = ps.executeQuery();
/*  64 */       while (rs.next()) {
/*  65 */         MapleGuildAlliance g = new MapleGuildAlliance(rs.getInt("id"));
/*  66 */         if (g.getId() > 0) {
/*  67 */           ret.add(g);
         }
       }
/*  70 */       rs.close();
/*  71 */       ps.close();
/*  72 */     } catch (SQLException se) {
/*  73 */       log.error("[MapleGuildAlliance] 从数据库中加载家族联盟信息出错." + se);
     }
/*  75 */     return ret;
   }

   public static int createToDb(int leaderId, String name, int guild1, int guild2) {
/*  79 */     int ret = -1;
/*  80 */     if (name.length() > 12) {
/*  81 */       return ret;
     }
/*  83 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  84 */       PreparedStatement ps = con.prepareStatement("SELECT id FROM alliances WHERE name = ?");
/*  85 */       ps.setString(1, name);
/*  86 */       ResultSet rs = ps.executeQuery();

/*  88 */       if (rs.first()) {
/*  89 */         rs.close();
/*  90 */         ps.close();
/*  91 */         return ret;
       }
/*  93 */       ps.close();
/*  94 */       rs.close();

/*  96 */       ps = con.prepareStatement("INSERT INTO alliances (name, guild1, guild2, leaderid) VALUES (?, ?, ?, ?)", 1);
/*  97 */       ps.setString(1, name);
/*  98 */       ps.setInt(2, guild1);
/*  99 */       ps.setInt(3, guild2);
/* 100 */       ps.setInt(4, leaderId);
/* 101 */       ps.execute();
/* 102 */       rs = ps.getGeneratedKeys();
/* 103 */       if (rs.next()) {
/* 104 */         ret = rs.getInt(1);
       }
/* 106 */       rs.close();
/* 107 */       ps.close();
/* 108 */     } catch (SQLException se) {
/* 109 */       log.error("[MapleGuildAlliance] 创建家族联盟出错." + se);
     }
/* 111 */     return ret;
   }

   public int getNoGuilds() {
/* 115 */     int ret = 0;
/* 116 */     for (int i = 0; i < this.capacity; i++) {
/* 117 */       if (this.guilds[i] > 0) {
/* 118 */         ret++;
       }
     }
/* 121 */     return ret;
   }

   public boolean deleteAlliance() {
/* 125 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {

/* 127 */       for (int i = 0; i < getNoGuilds(); i++) {
/* 128 */         PreparedStatement preparedStatement = con.prepareStatement("UPDATE characters SET alliancerank = 5 WHERE guildid = ?");
/* 129 */         preparedStatement.setInt(1, this.guilds[i]);
/* 130 */         preparedStatement.execute();
/* 131 */         preparedStatement.close();
       }

/* 134 */       PreparedStatement ps = con.prepareStatement("DELETE FROM alliances WHERE id = ?");
/* 135 */       ps.setInt(1, this.allianceid);
/* 136 */       ps.execute();
/* 137 */       ps.close();
/* 138 */     } catch (SQLException se) {
/* 139 */       log.error("[MapleGuildAlliance] 解散家族联盟出错." + se);
/* 140 */       return false;
     }
/* 142 */     return true;
   }

   public void broadcast(byte[] packet) {
/* 146 */     broadcast(packet, -1, GAOp.NONE, false);
   }

   public void broadcast(byte[] packet, int exception) {
/* 150 */     broadcast(packet, exception, GAOp.NONE, false);
   }

   public void broadcast(byte[] packet, int exceptionId, GAOp op, boolean expelled) {
/* 154 */     if (op == GAOp.DISBAND) {
/* 155 */       WorldAllianceService.getInstance().setOldAlliance(exceptionId, expelled, this.allianceid);
/* 156 */     } else if (op == GAOp.NEWGUILD) {
/* 157 */       WorldAllianceService.getInstance().setNewAlliance(exceptionId, this.allianceid);
     } else {
/* 159 */       WorldAllianceService.getInstance().sendGuild(packet, exceptionId, this.allianceid);
     }
   }

   public boolean disband() {
/* 164 */     boolean ret = deleteAlliance();
/* 165 */     if (ret) {
/* 166 */       broadcast(null, -1, GAOp.DISBAND, false);
     }
/* 168 */     return ret;
   }

   public void saveToDb() {
/* 172 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 173 */       PreparedStatement ps = con.prepareStatement("UPDATE alliances SET guild1 = ?, guild2 = ?, guild3 = ?, guild4 = ?, guild5 = ?, rank1 = ?, rank2 = ?, rank3 = ?, rank4 = ?, rank5 = ?, capacity = ?, leaderid = ?, notice = ? WHERE id = ?");
/* 174 */       for (int i = 0; i < 5; i++) {
/* 175 */         ps.setInt(i + 1, (this.guilds[i] < 0) ? 0 : this.guilds[i]);
/* 176 */         ps.setString(i + 6, this.ranks[i]);
       }
/* 178 */       ps.setInt(11, this.capacity);
/* 179 */       ps.setInt(12, this.leaderid);
/* 180 */       ps.setString(13, this.notice);
/* 181 */       ps.setInt(14, this.allianceid);
/* 182 */       ps.executeUpdate();
/* 183 */       ps.close();
/* 184 */     } catch (SQLException se) {
/* 185 */       log.error("[MapleGuildAlliance] 保存家族联盟出错." + se);
     }
   }

   public void setRank(String[] ranks) {
/* 190 */     this.ranks = ranks;
/* 191 */     broadcast(GuildPacket.getAllianceUpdate(this));
/* 192 */     saveToDb();
   }

   public String getRank(int rank) {
/* 196 */     return this.ranks[rank - 1];
   }

   public String[] getRanks() {
/* 200 */     return this.ranks;
   }

   public String getNotice() {
/* 204 */     return this.notice;
   }

   public void setNotice(String newNotice) {
/* 208 */     this.notice = newNotice;
/* 209 */     broadcast(GuildPacket.getAllianceUpdate(this));
/* 210 */     broadcast(MaplePacketCreator.serverNotice(5, "* 联盟公告 : " + newNotice));
/* 211 */     saveToDb();
   }

   public int getGuildId(int i) {
/* 215 */     return this.guilds[i];
   }

   public int getId() {
/* 219 */     return this.allianceid;
   }

   public String getName() {
/* 223 */     return this.name;
   }

   public int getCapacity() {
/* 227 */     return this.capacity;
   }

   public boolean setCapacity() {
/* 231 */     if (this.capacity >= 5) {
/* 232 */       return false;
     }
/* 234 */     this.capacity++;
/* 235 */     broadcast(GuildPacket.getAllianceUpdate(this));
/* 236 */     saveToDb();
/* 237 */     return true;
   }

   public boolean addGuild(int guildid) {
/* 241 */     if (getNoGuilds() >= getCapacity()) {
/* 242 */       return false;
     }
/* 244 */     this.guilds[getNoGuilds()] = guildid;
/* 245 */     saveToDb();
/* 246 */     broadcast(null, guildid, GAOp.NEWGUILD, false);
/* 247 */     return true;
   }

   public boolean removeGuild(int guildid, boolean expelled) {
/* 251 */     return removeGuild(guildid, expelled, false);
   }

   public boolean removeGuild(int guildid, boolean expelled, boolean isNull) {
/* 255 */     for (int i = 0; i < getNoGuilds(); i++) {
/* 256 */       if (this.guilds[i] == guildid) {
/* 257 */         if (!isNull) {
/* 258 */           broadcast(null, guildid, GAOp.DISBAND, expelled);
         }
/* 260 */         if (i > 0 && i != getNoGuilds() - 1) {
/* 261 */           for (int x = i + 1; x < getNoGuilds(); x++) {
/* 262 */             if (this.guilds[x] > 0) {
/* 263 */               this.guilds[x - 1] = this.guilds[x];
/* 264 */               if (x == getNoGuilds() - 1) {
/* 265 */                 this.guilds[x] = -1;
               }
             }
           }
         } else {
/* 270 */           this.guilds[i] = -1;
         }
/* 272 */         if (i == 0) {
/* 273 */           return disband();
         }
/* 275 */         broadcast(GuildPacket.getAllianceUpdate(this));
/* 276 */         broadcast(GuildPacket.getGuildAlliance(this));
/* 277 */         saveToDb();
/* 278 */         return true;
       }
     }

/* 282 */     return false;
   }

   public int getLeaderId() {
/* 286 */     return this.leaderid;
   }

   public boolean setLeaderId(int c) {
/* 290 */     return setLeaderId(c, false);
   }

   public boolean setLeaderId(int c, boolean sameGuild) {
/* 294 */     if (this.leaderid == c) {
/* 295 */       return false;
     }

/* 298 */     int g = -1;
/* 299 */     String leaderName = null;
/* 300 */     for (int i = 0; i < getNoGuilds(); i++) {
/* 301 */       MapleGuild g_ = WorldGuildService.getInstance().getGuild(this.guilds[i]);
/* 302 */       if (g_ != null) {
/* 303 */         MapleGuildCharacter newLead = g_.getMGC(c);
/* 304 */         MapleGuildCharacter oldLead = g_.getMGC(this.leaderid);
/* 305 */         if (newLead != null && oldLead != null && !sameGuild) {
/* 306 */           return false;
         }
/* 308 */         if (newLead != null && newLead.getGuildRank() == 1 && newLead.getAllianceRank() == 2) {
/* 309 */           g_.changeARank(c, 1);
/* 310 */           g = i;
/* 311 */           leaderName = newLead.getName();
         }
/* 313 */         if (oldLead != null && oldLead.getGuildRank() == 1 && oldLead.getAllianceRank() == 1) {
/* 314 */           g_.changeARank(this.leaderid, 2);
         }
       }
     }
/* 318 */     if (g == -1) {
/* 319 */       return false;
     }
/* 321 */     int oldGuild = this.guilds[g];
/* 322 */     this.guilds[g] = this.guilds[0];
/* 323 */     this.guilds[0] = oldGuild;
/* 324 */     if (leaderName != null) {
/* 325 */       broadcast(MaplePacketCreator.serverNotice(5, "* 联盟公告 : " + leaderName + " 成为了家族联盟族长."));
     }
/* 327 */     broadcast(GuildPacket.changeAllianceLeader(this.allianceid, this.leaderid, c));
/* 328 */     broadcast(GuildPacket.updateAllianceLeader(this.allianceid, this.leaderid, c));
/* 329 */     broadcast(GuildPacket.getAllianceUpdate(this));
/* 330 */     broadcast(GuildPacket.getGuildAlliance(this));
/* 331 */     this.leaderid = c;
/* 332 */     saveToDb();
/* 333 */     return true;
   }

   public boolean changeAllianceRank(int chrId, int change) {
/* 337 */     if (this.leaderid == chrId || change < 0 || change > 1) {
/* 338 */       return false;
     }
/* 340 */     for (int i = 0; i < getNoGuilds(); i++) {
/* 341 */       MapleGuild guild = WorldGuildService.getInstance().getGuild(this.guilds[i]);
/* 342 */       if (guild != null) {
/* 343 */         MapleGuildCharacter chr = guild.getMGC(chrId);
/* 344 */         if (chr != null && chr.getAllianceRank() > 2) {
/* 345 */           if ((change == 0 && chr.getAllianceRank() >= 5) || (change == 1 && chr.getAllianceRank() <= 3)) {
/* 346 */             return false;
           }
/* 348 */           guild.changeARank(chrId, chr.getAllianceRank() + ((change == 0) ? 1 : -1));
/* 349 */           return true;
         }
       }
     }
/* 353 */     return false;
   }

   private enum GAOp
   {
/* 358 */     NONE, DISBAND, NEWGUILD;
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\guild\MapleGuildAlliance.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */